<template>
  <div>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-select
          clearable
          filterable
          @change="change()"
          placeholder="请选择/搜索"
          v-model="dictType"
        >
          <el-option
            v-for="item in DictTypeNamesList"
            :key="item.dictType"
            :label="item.dictName"
            :value="item.dictType"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="findDictData()"
        >查询</el-button
      >

      <el-button
        type="success"
        icon="el-icon-circle-plus-outline"
        @click="toAddDictType()"
        v-if="activeName == 'second'"
        >添加字典类型</el-button
      >

      <el-button
        type="success"
        icon="el-icon-circle-plus-outline"
        @click="addDictData()"
        v-if="activeName == 'first'"
        >添加字典数据</el-button
      >

      <el-button
        type="danger"
        icon="el-icon-delete"
        @click="batchDeletess()"
        v-if="activeName == 'first'"
        >批量删除字典数据</el-button
      >

      <el-button
        type="danger"
        icon="el-icon-delete"
        @click="batchselectType()"
        v-if="activeName == 'second'"
        >批量删除字典类型</el-button
      >
    </el-form>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="字典数据" name="first">
        <el-table
          ref="multipleTable"
          :data="dictDataList"
          tooltip-effect="dark"
          style="width: 100%"
          @select="selectItem"
          @select-all="selectAll"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="dictSort" label="字典排序" width="120">
          </el-table-column>
          <el-table-column prop="dictLabel" label="字典标签"> </el-table-column>
          <el-table-column prop="dictValue" label="字典键值"> </el-table-column>
          <el-table-column prop="dictType" label="字典类型"> </el-table-column>
          <el-table-column prop="isDefault" label="是否默认">
            <template slot-scope="scope">
              <div>
                {{ scope.row.isDefault | defaultIntercept }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <div>
                {{ scope.row.status | stateFilter }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="createBy" label="创建者"></el-table-column>
          <el-table-column prop="createTime" label="创建时间">
          </el-table-column>
          <el-table-column prop="remark" label="备注"> </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <div>
                <el-button
                  @click="toUpdateDictData(scope.row)"
                  type="primary"
                  size="mini"
                  icon="el-icon-edit"
                  >修改</el-button
                >
                <el-button
                  @click="toDeleteDictData(scope.row.dictCode)"
                  type="danger"
                  size="mini"
                  icon="el-icon-delete"
                  >删除</el-button
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-switch
                style="display: block"
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="开启"
                inactive-text="停用"
                active-value="0"
                inactive-value="1"
                @change="setStatusAndDictData(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
        <!-- 字典添加数据的弹出框 -->
        <el-dialog title="字典添加数据" :visible.sync="outerVisible1">
          <el-dialog
            width="30%"
            title="内层 Dialog"
            :visible.sync="innerVisible"
            append-to-body
          >
          </el-dialog>
          <el-form :rules="rules" :model="repo" ref="repoRef">
            <el-form-item label="字典列表">
              <el-select
                placeholder="请字典类型"
                v-model="dictDataAddList.dictType"
              >
                <el-option
                  v-for="item in DictTypeNamesList"
                  :key="item.dictType"
                  :label="item.dictName"
                  :value="item.dictType"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="字典排序" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictDataAddList.dictSort"
              ></el-input>
            </el-form-item>
            <el-form-item label="字典标签" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictDataAddList.dictLabel"
              ></el-input>
            </el-form-item>
            <el-form-item label="字典键值" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictDataAddList.dictValue"
              ></el-input>
            </el-form-item>
            <el-select
              v-model="dictDataAddList.isDefault"
              placeholder="请选择是否默认"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>

            <el-form-item label="创建者" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictDataAddList.createBy"
              ></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictDataAddList.remark"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="outerVisible1 = false">取 消</el-button>
            <el-button type="primary" @click="addDictDatass()">提交</el-button>
          </div>
        </el-dialog>

        <!-- 字典修改数据的弹出框 -->
        <el-dialog title="字典添加数据" :visible.sync="toUpdateDictDataVisible">
          <el-dialog
            width="30%"
            title="内层 Dialog"
            :visible.sync="innerVisible"
            append-to-body
          >
          </el-dialog>
          <el-form :rules="rules" :model="repo" ref="repoRef">
            <el-form-item label="字典列表">
              <el-select
                placeholder="请字典类型"
                v-model="dictDataAddList.dictType"
              >
                <el-option
                  v-for="item in DictTypeNamesList"
                  :key="item.dictType"
                  :label="item.dictName"
                  :value="item.dictType"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="字典排序" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictDataAddList.dictSort"
              ></el-input>
            </el-form-item>
            <el-form-item label="字典标签" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictDataAddList.dictLabel"
              ></el-input>
            </el-form-item>
            <el-form-item label="字典键值" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictDataAddList.dictValue"
              ></el-input>
            </el-form-item>
            <el-select
              v-model="dictDataAddList.isDefault"
              placeholder="请选择是否默认"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>

            <el-form-item label="创建者" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictDataAddList.createBy"
              ></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictDataAddList.remark"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="outerVisible1 = false">取 消</el-button>
            <el-button type="primary" @click="updateDictDatass()"
              >提交</el-button
            >
          </div>
        </el-dialog>
      </el-tab-pane>

      <el-tab-pane label="字典类型" name="second">
        <el-table
          ref="multipleTable"
          :data="dictTypeList"
          tooltip-effect="dark"
          style="width: 100%"
          @select="selectItem"
          @select-all="selectAll"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="dictId" label="字典主键" width="120">
          </el-table-column>
          <el-table-column prop="dictName" label="字典名称" width="120">
          </el-table-column>
          <el-table-column prop="dictType" label="字典类型"> </el-table-column>
          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <div>
                {{ scope.row.status | stateFilter }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="createBy" label="创建者"></el-table-column>
          <el-table-column prop="createTime" label="创建时间">
          </el-table-column>
          <el-table-column prop="remark" label="备注"> </el-table-column>

          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <div>
                <el-button
                  @click="toUpdateDictType(scope.row)"
                  type="primary"
                  size="mini"
                  icon="el-icon-edit"
                  >修改</el-button
                >
                <el-button
                  @click="deleteDictTypess(scope.row.dictId)"
                  type="danger"
                  size="mini"
                  icon="el-icon-delete"
                  >删除</el-button
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-switch
                style="display: block"
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="开启"
                inactive-text="停用"
                active-value="0"
                inactive-value="1"
                @change="setStatusAndDictType(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
        <!-- 字典添加类型的弹出框 -->
        <el-dialog title="字典添加类型" :visible.sync="outerVisible2">
          <el-dialog
            width="30%"
            title="内层 Dialog"
            :visible.sync="innerVisible"
            append-to-body
          >
          </el-dialog>
          <el-form :rules="rules" :model="repo" ref="repoRef">
            <el-form-item label="字典名称" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictTypeAddList.dictName"
              ></el-input>
            </el-form-item>
            <el-form-item label="字典类型" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictTypeAddList.dictType"
              ></el-input>
            </el-form-item>
            <el-form-item label="创建者" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictTypeAddList.createBy"
              ></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictTypeAddList.remark"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="outerVisible2 = false">取 消</el-button>
            <el-button type="primary" @click="addDictType()">提交</el-button>
          </div>
        </el-dialog>

        <!-- 字典修改类型的弹出框 -->
        <el-dialog title="字典添加类型" :visible.sync="toUpdateDictTypeVisible">
          <el-dialog
            width="30%"
            title="内层 Dialog"
            :visible.sync="innerVisible"
            append-to-body
          >
          </el-dialog>
          <el-form :rules="rules" :model="repo" ref="repoRef">
            <el-form-item label="字典名称" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictTypeAddList.dictName"
              ></el-input>
            </el-form-item>
            <el-form-item label="字典类型" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictTypeAddList.dictType"
              ></el-input>
            </el-form-item>
            <el-form-item label="创建者" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictTypeAddList.createBy"
              ></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                autocomplete="off"
                v-model="dictTypeAddList.remark"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="outerVisible2 = false">取 消</el-button>
            <el-button type="primary" @click="updateDictTypess()"
              >提交</el-button
            >
          </div>
        </el-dialog>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  findDictTypeNames,
  findAll,
  addDictDatas,
  addDictType,
  findAllDictType,
  updateDictData,
  updateDictType,
  updateStatusAndType,
  updateDictDataAndStatus,
  deleteDictData,
  deleteDictType,
  batchDelete,
  batchDeleteType,
} from "@/api/dict";
import { update } from "@/api/repo";
export default {
  data() {
    return {
      DictTypeNamesList: [],
      dictType: "",
      dictDataList: [],
      dictTypeList: [],
      activeName: "first",
      outerVisible1: false,
      outerVisible2: false,
      toUpdateDictTypeVisible: false,
      toUpdateDictDataVisible: false,
      dictDataAddList: {},
      dictTypeAddList: {},
      dictDatadeleteList: [],
      dictTypedeleteList: [],
      options: [
        {
          value: "Y",
          label: "是",
        },
        {
          value: "N",
          label: "否",
        },
      ],
    };
  },
  methods: {
    findDictTypeNames() {
      findDictTypeNames().then((res) => {
        this.DictTypeNamesList = res.data;
      });
    },
    findDictData() {
      findAll({ dictType: this.dictType }).then((res) => {
        this.dictDataList = res.data;
        this.findAllDictType();
      });
    },
    findAllDictType() {
      findAllDictType({ dictType: this.dictType }).then((res) => {
        this.dictTypeList = res.data;
      });
    },
    addDictData() {
      this.outerVisible1 = true;
      this.activeName = "first";
    },
    addDictDatass() {
      addDictDatas(this.dictDataAddList).then((res) => {
        this.outerVisible1 = false;
        this.findDictData();
      });
    },
    toAddDictType() {
      this.outerVisible2 = true;
      this.activeName = "second";
    },
    addDictType() {
      addDictType(this.dictTypeAddList).then((res) => {
        this.findDictTypeNames();
        this.outerVisible2 = false;
        this.findDictData();
      });
    },
    change() {
      this.findDictData();
      this.findAllDictType();
    },
    toUpdateDictType(row) {
      this.toUpdateDictTypeVisible = true;
      this.dictTypeAddList = row;
    },
    toUpdateDictData(row) {
      this.toUpdateDictDataVisible = true;
      this.dictDataAddList = row;
    },
    updateDictDatass() {
      updateDictData(this.dictDataAddList).then((res) => {
        this.toUpdateDictDataVisible = false;
        this.findDictData();
        this.findAllDictType();
      });
    },
    updateDictTypess() {
      updateDictType(this.dictTypeAddList).then((res) => {
        this.toUpdateDictTypeVisible = false;
        this.findDictData();
        this.findAllDictType();
      });
    },
    setStatusAndDictType(row) {
      updateStatusAndType(row.dictId).then((res) => {
        this.dictTypeList.status = res.data.status;
      });
    },
    setStatusAndDictData(row) {
      updateDictDataAndStatus(row.dictCode).then((res) => {
        this.dictDataList.status = res.data.status;
      });
    },
    toDeleteDictData(row) {
      this.$confirm("此操作将永久删除该字典数据，是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteDictData(row).then((res) => {
          if (res.code == 0) {
            this.findDictData();
            this.findAllDictType();
          }
          this.$message({
            message: res.msg,
            type: res.code == 0 ? "success" : "error",
          });
        });
      });
    },
    deleteDictTypess(row) {
      this.$confirm("此操作将永久删除该字典类型，是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteDictType(row).then((res) => {
          if (res.code == 0) {
            this.findDictData();
            this.findAllDictType();
          }
          this.$message({
            message: res.msg,
            type: res.code == 0 ? "success" : "error",
          });
        });
      });
    },
    batchDeletess() {
      this.$confirm("此操作将永久删除该字典数据，是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        batchDelete(this.dictDatadeleteList).then((res) => {
          if (res.code == 0) {
            this.findDictData();
            this.findAllDictType();
          }
          this.$message({
            message: res.msg,
            type: res.code == 0 ? "success" : "error",
          });
        });
      });
    },
    selectAll(selection) {
      this.dictDatadeleteList = selection;
      this.dictTypedeleteList = selection;
    },
    selectItem(selection, row) {
      this.dictDatadeleteList = selection;
      this.dictTypedeleteList = selection;
    },
    batchselectType() {
      this.$confirm("此操作将永久删除该字典类型，是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        batchDeleteType(this.dictTypedeleteList).then((res) => {
          if (res.code == 0) {
            this.findDictData();
            this.findAllDictType();
            this.findDictTypeNames();
          }
          this.$message({
            message: res.msg,
            type: res.code == 0 ? "success" : "error",
          });
        });
      });
    },
  },

  created() {
    this.findDictTypeNames();
    this.findDictData();
    this.findAllDictType();
  },
};
</script>

<style>
</style>